var express = require('express');
var router = express.Router();
var mysql = require("./tool/mysql");
var url = require('url')
/* GET users listing. */
router.get('/', function(req, res, next) {
  // res.send('产品列表');
  mysql.connect(function(db){
    console.log("success-newList");
    var querObj = {};
    var showObj = {
      _id: 0,
      id: 1,
      title:1,
      year:1,
      images_small:1,
      genres:1,
      rating:1
    };
    // var skipNum = 0;
    // mysql.findSkip(db,"newList",querObj,showObj,skipNum,function(result){
    mysql.find(db,"newList",querObj,showObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"锋影-电影列表",
          activeIndex:1,
          isBoss: false,
          result: result,
          sort : 0
        }
        // res.render("list",obj);
        res.send(result);
        // console.log(result);
        db.close();
    })
  })
  // res.render("list",{title:"锋影-电影列表",activeIndex:1,isBoss: false});
});

router.get('/listSort', function(req, res, next) {
  // res.send('产品列表');
  var sortType = url.parse(req.url,true).query.sortType;
  var type = url.parse(req.url,true).query.type;
  mysql.connect(function(db){
    console.log("success-sortList");
    var querObj = {};
    var showObj = {
      _id: 0,
      id: 1,
      title:1,
      year:1,
      images_small:1,
      genres:1,
      rating:1
    };
    var sortObj = {}
    if (sortType == "rating") {
      sortObj = {
        rating:type*1 
      }
    }
    
    // mysql.findSkip(db,"newList",querObj,showObj,skipNum,function(result){
    mysql.findSort(db,"newList",querObj,showObj,sortObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"锋影-电影列表正序",
          activeIndex:1,
          isBoss: false,
          result: result,
          sort:type
        }
        res.render("list",obj);
        // console.log(result);
        db.close();
    })
  })
  // res.render("list",{title:"锋影-电影列表",activeIndex:1,isBoss: false});
});
// 进入删除页面
router.get('/deleteMovie',function(req, res, next){
  var id = url.parse(req.url,true).query.id;
  obj ={
    title:"锋影-删除电影",
    activeIndex:1,
    isBoss: false,
    id :id
  };
  mysql.connect(function(db){
    var deleteObj = {
      id :id
    }
    mysql.deleteOne(db,'newList',deleteObj,function(result){
        res.send("<script>window.location.href='/list'</script>");
        db.close();
    })
  })
})
// 进入更新页面
router.get('/updateMovie', function(req, res, next) {
  var id = url.parse(req.url,true).query.id;
  // obj ={
  //   title:"锋影-更新电影",
  //   activeIndex:1,
  //   isBoss: false,
  //   id :id
  // }
  // 依据id查询本电影
  mysql.connect(function(db){
    console.log("success-newList");
    var querObj = {id : id};
    var showObj = {
      _id: 0,
    }
    mysql.find(db,"newList",querObj,showObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"锋影-电影列表",
          activeIndex:1,
          isBoss: false,
          result: result
        }
        res.render("updateMovie",obj);
        console.log(result);
        db.close();
    })
  })
  // res.render('updateMovie',obj)
});


// 更新数据的方法
router.post('/updateMovieAction', function(req, res, next) {
  console.log(req.body);
  
  var updateObj = req.body;
  var whereObj = {
    id: updateObj.id
  }
  mysql.connect(function(db){
    mysql.updateOne(db,"newList",whereObj,updateObj,function(result){
      res.send("<script>window.location.href='/list'</script>");
      db.close();
    })
  })
 
});

// 跳转到添加页面
router.get('/addMovie', function(req, res, next) {
  obj ={
    title:"锋影-添加电影",
    activeIndex:1,
    isBoss: false,
  }
  res.render("addMovie",obj);
});

// 添加电影的方法
router.post('/addMovieAction', function(req, res, next) {
  console.log(req.body);
  
  mysql.connect(function(db){
    var insertData = req.body;
    mysql.insert(db,"newList",insertData,function(result){
      res.send("<script>window.location.href='/list'</script>");
      db.close();
    })
  })
 
});

// 分页功能
router.get('/fenye', function(req, res, next) {
  // res.send('产品列表');
  var pageCode = url.parse(req.url,true).query.pageCode*1;
  var limitNum = url.parse(req.url,true).query.limitNum*1;
  mysql.connect(function(db){
    console.log("success-newList");
    var querObj = {};
    var showObj = {
      _id: 0,
      id: 1,
      title:1,
      year:1,
      images_small:1,
      genres:1,
      rating:1
    };
    var skipNum =pageCode*limitNum;
    
    mysql.find(db,"newList",querObj,showObj,function(resultAll){

      mysql.findFenye(db,"newList",querObj,showObj,limitNum,skipNum,pageCode,function(result){
        var totalPages = Math.ceil(resultAll.length / limitNum);
            obj ={
              title:"锋影-电影列表分页",
              activeIndex:4,
              isBoss: false,
              result: result,
              sort : 0,
              allNum: resultAll.length,
              pageCode: pageCode,
              totalPages: totalPages
            }
            // res.render("listfenye",obj); //页面
            // console.log(result);
            res.send(result);  //接口
            // db.close();
        })
    })
  })
  // res.render("list",{title:"锋影-电影列表",activeIndex:1,isBoss: false});
});

module.exports = router;
